// AddressServiceImpl.java 实现类
package com.xgq.drink.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xgq.drink.entity.Address;
import com.xgq.drink.entity.R;
import com.xgq.drink.mapper.AddressMapper;
import com.xgq.drink.utils.RUtils;
import com.xgq.drink.service.IAddressService;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper, Address> implements IAddressService {

    @Override
    public R addAddress(Address address, String userId) {
        System.out.println("用户ID:"+userId);
        address.setUserId(Long.valueOf(userId));
        address.setIsDel(0);
        boolean saved = save(address);
        return saved ? RUtils.success("添加成功") : RUtils.fail("添加失败");
    }

    @Override
    public R updateAddress(Address address, Long userId) {
        address.setUserId(userId); // 防止越权修改
        boolean updated = update(address, new QueryWrapper<Address>()
                .eq("address_id", address.getAddressId())
                .eq("user_id", userId));
        return updated ? RUtils.success("更新成功") : RUtils.fail("更新失败");
    }

    @Override
    public R getAddressList(Long userId) {
        System.out.println("用户ID:"+userId);
        List<Address> addresses = list(new QueryWrapper<Address>()
                .eq("user_id", userId)
                .eq("is_del", 0)); // 只查询未删除的地址
        return RUtils.success("查询成功", addresses);
    }
}